<?php
namespace core;
class config extends \api\core\config{

    private $com;//组件名
    public static $config;//项目配置
    public static function getFrame($config, $call) {
        $module_file = PRO_PATH . '/app/' . $call['app'] . '/modules/' . $call['module'] . '/config/config.php';
        $app_file = PRO_PATH . '/app/' . $call['app'] . '/config/config.php';
        $pro_file = PRO_PATH.'/config/config.php';
        $pconfig = self::getRoute($config, $pro_file);
        if (is_file($app_file)) {
            require_once $app_file;
            $aconfig = self::chkConfig($pconfig, $config);
            $pconfig = self::getRoute($aconfig, $app_file);
            if (is_file($module_file)) {
                require_once $module_file;
                $mconfig = self::chkConfig($pconfig, $config);
                $pconfig = self::getRoute($mconfig, $module_file);           
            }
        }
        return $pconfig;
    }
    public static function getAsset($asset){
        $pro_file = PRO_PATH.'/config/config.php';
        $asset_file = ASSET_PATH.'/'.$asset.'/config/config.php';
        if(is_file($pro_file)){
            require $pro_file;
            $pconfig = $config;
            if(is_file($asset_file)){
                require_once $asset_file;
                $pconfig = self::chkConfig($pconfig, $config);
            }
        }
        return $pconfig;
    }
    public static function getRoute($config,$file){
        $rou_file = dirname($file).'/routers.php';
        if(is_file($rou_file)){
            require $rou_file;         
            if(isset($config['router']['routers']) && is_array($config['router']['routers'])){                
                $config['router']['routers'] = array_merge($config['router']['routers'],$routers);
            }else{
                $config['router']['routers'] = $routers;
            }
            return $config;
        }else{
            return $config;
        }
    }
    public static function chkConfig($oconf, $nconf) {
        $config = array_merge_recursive($oconf, $nconf);
        $config = self::delConfig($config);
        return $config;
    }
    public static function delConfig($config) {
        foreach ($config as $key => $value) {
            foreach ($value as $k => $v) {
                if (is_array($v)) {
                    foreach ($v as $j => $i) {
                        if (is_int($j) || is_integer($j)) {
                            $config[$key][$k] = $v[count($v) - 1];
                        }
                    }
                }
            }
        }
        return $config;
    }
    public static function setGlobal(){
        if(!defined("ROOT_PATH")){
            define("ROOT_PATH",dirname(dirname(dirname(__FILE__))).'/web');
        }
        if(!defined("PRO_PATH")){
            define("PRO_PATH",dirname(ROOT_PATH).'/Project');
        }
        if(!defined("APP_PATH")){
            define("APP_PATH",PRO_PATH.'/app');
        }
        if(!defined("ASSET_PATH")){
            define("ASSET_PATH",PRO_PATH.'/assets');
        }
        if(!defined("TPL_PATH")){
            define("TPL_PATH",PRO_PATH.'/templates');
        }
    }
}

?>
